package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.yami.shop.bean.model.OrderFriends;
import com.yami.shop.bean.model.Params;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.OrderFriendsMapper;
import com.yami.shop.dao.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/damin/order")
@Api(tags = "用户充值订单管理")
public class OrderController {
        @Autowired
        private OrderFriendsMapper orderFriendsMapper;
        @Autowired
        private UserMapper userMapper;

        @GetMapping("getOrderFriends")
        @ApiOperation(value = "获取订单列表",tags = "获取订单列表")
        public ResponseEntity<IPage<OrderFriends>> getOrderFriends(PageParam page, Params params){
                String str1 = "";
                String str2 = "";
                if (params.getMaxTime() != null){
                        str1 = params.getMaxTime().substring(0, params.getMaxTime().indexOf("T"));
                }
                if (params.getMinTime() != null){
                        str2 = params.getMinTime().substring(0, params.getMinTime().indexOf("T"));
                }
                IPage<OrderFriends> ors = orderFriendsMapper.selectPage(page,new QueryWrapper<OrderFriends>()
                .eq(params.getIsPayed() != null,"is_payed",params.getIsPayed())//是否已付款
                .le(params.getMaxTime() != null,"create_time",str1)//订购时间
                .ge(params.getMinTime() != null,"create_time",str2)//订购时间
                .orderByDesc("create_time"));
                for (OrderFriends or : ors.getRecords()){
                        String userName = userMapper.selectById(or.getUserId()).getNickName();
                        or.setUserName(userName);
                }
                return ResponseEntity.ok(ors);
        }

        @GetMapping("getStatistics")
        @ApiOperation(value = "总额计算",tags = "总额计算")
        public ResponseEntity<Double> getStatistics(Params params){
                String str1 = "";
                String str2 = "";
                if (params.getMaxTime() != null){
                        str1 = params.getMaxTime().substring(0, params.getMaxTime().indexOf("T"));
                }
                if (params.getMinTime() != null){
                        str2 = params.getMinTime().substring(0, params.getMinTime().indexOf("T"));
                }
                List<OrderFriends> orss = orderFriendsMapper.selectList(new QueryWrapper<OrderFriends>()
                        .eq(params.getIsPayed() != null,"is_payed",params.getIsPayed())//是否已付款
                        .le(params.getMaxTime() != null,"create_time",str1)//订购时间
                        .ge(params.getMinTime() != null,"create_time",str2)//订购时间
                        .orderByDesc("create_time"));
                Double statistics = 0.00;
                for (OrderFriends or : orss){
                        statistics = statistics+or.getActualTotal();
                }
                return ResponseEntity.ok(statistics);
        }



}
